package com.sapitest.utils;

import java.io.UnsupportedEncodingException;
import java.util.Locale;
import java.util.ResourceBundle;

/**
 * @date 2019-01-13
 * @author Liuxu
 * @version 1.0.0
 * @since jdk1.8
 */
public class ConfigFile {

    /**
     * 据配置文件config.properties文件读取系统设置参数
     * @param key 参数key
     * @return String
     */
    public static String getSystemParameter(String key) {
        ResourceBundle bundle = ResourceBundle.getBundle("config", Locale.CHINA);
        return trans2UTF8(bundle, key);
    }

    /**
     * 根据配置文件application.properties文件读取测试参数
     * @param key 参数key
     * @return  String
     */
    public static String getTestParameter(String key){
        ResourceBundle bundle_test_parameter = ResourceBundle.getBundle("application", Locale.CHINA);
            return trans2UTF8(bundle_test_parameter, key);
    }

    /**
     * properties配置文件编码格式由ISO-8859-1转为UTF8
     * @param bundle    ResourceBundle
     * @param key   参数key
     * @return  String
     */
    private static String trans2UTF8(ResourceBundle bundle, String key){
        try {
            return new String(bundle.getString(key).getBytes("ISO-8859-1"), "UTF8");
        } catch (UnsupportedEncodingException e) {
            return bundle.getString(key);
        }
    }
}
